/**
 * @author xh
 * 双重锁的 getWife() 方法
 */
public class Wife4 {
    // 防止编译器自行优化代码
    private volatile static Wife4 wife;

    private Wife4() {
    }

    public static Wife4 getWife() {
        // 第一个检验锁，如果不为空直接返回实例对象，为空才进入下一步
        if (wife == null) {
            synchronized (Wife4.class) {
                // 第二个检验锁，因为可能有多个线程进入到 if 语句内
                if (wife == null) {
                    wife = new Wife4();
                }
            }
        }
        return wife;
    }
}
